import Vue from 'vue'
import PBtn from "./components/PBtn/index.js"
import Table from './components/Table/index.js'
import { cloneDeep } from "lodash";
import { multipleAllNumber } from "./components/Form/select.vue";
export default {
  data() {
    return {
      isCn: window.localStorage.getItem('language') === 'zh_CN',
      domin: window.siteHeaders.staticURL,
      siteHeaders: window.siteHeaders,
    }
  },
  computed: {
    // 所有路由权限
    routerQuxianList() {
      return this.$store.state.routerQuxian.split(',');
    },
    // 浏览器宽度
    clientWidth() {
      return this.$store.state.clientWidth;
    },
    // 浏览器宽度
    pageNew() {
      return this.$store.state.pageNew;
    },
    // 浏览器宽度
    pageArray() {
      return this.$store.state.pageArray;
    },
    // 路由参数
    routeQuery(){
      const { query = {} } = this.$route || {};
      return query
    }
  },
  methods: {
    // v-if 代替指令
    vif(bl) {
      return (node) => bl ? node : null
    },
    //复制
    doCopy(text) {
      this.$copyText(text).then(() => {
        this.$alert({
          center: $AONE_LAN.copySuccess
        })
      }, () => {
        this.$alert({
          center: $AONE_LAN.copyFail,
          type: 'fail'
        })
      })
    },
    /**
     * 生成按钮
     */
    createBtn(options) {
      return PBtn.createBtn.bind(this)(options)
    },
    /**
     * 生成标签按钮
     */
    createTag(options) {
      return PBtn.createTag.bind(this)(options)
    },
    /**
     * 生成链接样式按钮
     */
    createLink(options) {
      return PBtn.createLink.bind(this)(options)
    },
    /**
     * 带复制的链接按钮
     * @returns 
     */
    createCopy(options) {
      return PBtn.createCopy.bind(this)(options)
    },
    /**
     * 带编辑按钮
     * @param {*} options 
     * @returns 
     */
    createEdit(options) {
      return PBtn.createEdit.bind(this)(options)
    },
    // 随机颜色
    randColor(minNum = 0, maxNum = 230) {
      switch (arguments.length) {
        case 1:
          return parseInt(Math.random() * minNum + 1, 10)
        case 2:
          return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10)
        default:
          return 0
      }
    },
    // reg组合
    randRGBColor() {
      return 'rgb' + '(' + this.randColor(1, 230) + ',' + this.randColor(1, 230) + ',' + this.randColor(1, 230) + ')'
    },
    // 最近时间
    beautifyTime(timestamp) {
      if (timestamp === 0) return '--'
      var mistiming = Math.round(new Date() / 1000) - timestamp;

      var postfix = mistiming > 0 ? $AONE_LAN.before : $AONE_LAN.after
      mistiming = Math.abs(mistiming)
      var arrr = [
        $AONE_LAN.year,
        $AONE_LAN.month,
        $AONE_LAN.week,
        $AONE_LAN.day,
        $AONE_LAN.hour,
        $AONE_LAN.mins,
        $AONE_LAN.sec
      ];
      var arrn = [31536000, 2592000, 604800, 86400, 3600, 60, 1];
      for (var i = 0; i < 7; i++) {
        var inm = Math.floor(mistiming / arrn[i])
        if (inm != 0) {
          return inm + `${this.langEn ? ' ' : ''}` + arrr[i] + `${this.langEn ? ' ' : ''}` + postfix
        }
      }
    },
    // 创建表格
    createTable(option) {
      return Table.createTable.bind(this)(option)
    },
    // 内部格式化fordata
    _formatValue(columns, formData) {
      let copyFormData = cloneDeep(formData)
      if (Array.isArray(columns) && copyFormData) {
        for (let key in copyFormData) {
          // select multiple 里面增加了个全选项目，最后接口请求要去掉这个项的value = multipleAllNumber
          if (Array.isArray(copyFormData[key]) && copyFormData[key].includes(multipleAllNumber)) {
            const columnItem = columns.find(it => it.prop === key)
            if (columnItem && columnItem.comtype == "select" && columnItem.multiple) {
              copyFormData[key] = copyFormData[key].filter(it => it !== multipleAllNumber)
            }
          }
          // 删除 undefined   null   ""  值
          else if (copyFormData[key] === undefined || copyFormData[key] === null || copyFormData[key] === "") {
            delete copyFormData[key]
          }
          // 删除空数组
          // else if (Array.isArray(copyFormData[key]) && copyFormData[key].length == 0) {
          //   delete copyFormData[key]
          // }
        }
      }
      return copyFormData
    },
    setSelectionRowsById(ids, key = 'id', selected = true) {
      const xTable = this.$refs.table.$refs.xTable
      const rows = this.getAllTableData().filter(it => ids.includes(it[key]))
      xTable.clearSelection()
      return xTable.toggleRowSelection(rows.map(it => {
        return {
          row: it,
          selected: selected
        }
      }), selected);
    },
    getSelectionRows() {
      const xTable = this.$refs.table.$refs.xTable
      return xTable.getCheckboxRecords();
    },
    scrollBottom() {
      const xTable = this.$refs.table.$refs.xTable
      return xTable.scrollBottom();
    },
    getAllTableData() {
      return this.$refs.table.tableData.items;
    },
    showLoading() {
      return (this.$refs.table.config.isLoading = true);
    },
    hideLoading() {
      return (this.$refs.table.config.isLoading = false);
    },
    toggleRowExpansion(row, expanded) {
      const xTable = this.$refs.table.$refs.xTable
      xTable.toggleRowExpansion(row, expanded)
    },
    finishSummary() {
      this.$refs.table.config.totalSummaryLoading = false
    },
    iconLoading() {
      return <span
        class="aoneui-vxe-icon--refresh vxe-icon--refresh roll get-gt-spin"
      ></span>
    },
    // 刷新合计按钮
    createRefreshSummaryBtn(fn) {
      const that = this.$refs.table
      return !that.config.totalSummaryLoading ? this.createBtn([
        {
          label: $AONE_LAN.refresh,
          class: "anoe-button-refresh",
          onClick: () => {
            that.config.totalSummaryLoading = true
            this.$nextTick(() => {
              if (fn) {
                fn()
              }
              else {
                that.mergeRequestHandler.bind(that)()
              }
            })
          },
        },
      ]) : this.iconLoading()
    }
  }
}